﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WPay.Infrastructure.Unity
{
    /// <summary>
    /// 创建票据号
    /// </summary>
   public class BllNoHelper
    {
        /// <summary>
        /// 创建票据号
        /// </summary>
        /// <param name="cpnId">企业代码</param>
        /// <param name="bllTyp">单据类型</param>
        /// <returns></returns>
        public static string CreateBillNo(string cpnId, string bllTyp)
        {
            try
            {
                //企业代码、单据类型禁止为空
                if (string.IsNullOrWhiteSpace(cpnId) || string.IsNullOrWhiteSpace(bllTyp))
                    throw new UNException("企业代码或者单据类型禁止为空");
                //生成随机数
                var rd = new Random(Guid.NewGuid().GetHashCode());
                //4位，不足4位，左侧0补齐
                string str = string.Format("{0:d4}", rd.Next(0, 9999));
                //将票据类型转大写
                bllTyp = bllTyp.ToUpper();
                //单据号：单据类型(3位)+年月日时分秒毫秒(yyMMddHHmmssfff：15位)+随机数(4位)+企业代码(6位) 共28位
                string nowID = string.Empty;
                int chkNum = 0;
                //拼装号码
                switch (bllTyp)
                {
                    default:             
                        nowID = bllTyp + DateTime.Now.ToString("yyMMddHHmmssfff") + str + cpnId;
                        break;
                }
                return nowID;
            }
            catch (UNException ex)
            {
                throw new UNException(ex.Message);
            }
        }

    }
}
